package com.doit.day02;

import com.doit.day01.MyConfig;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.sql.*;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;

public class _04_Kafka2Mysql {
    public static void main(String[] args) throws Exception {
        //获取mysql的连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        PreparedStatement pps = conn.prepareStatement("insert into user_info values(?,?,?,?)");
        PreparedStatement offset_pps = conn.prepareStatement("INSERT INTO tp_offset VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE offset = ?;");
        //把自动开启事务给关闭
        conn.setAutoCommit(false);
        PreparedStatement get_offset = conn.prepareStatement("select offset from tp_offset where group_id = 'g01' and topic = ? and `partition` = ?");


        Properties props = new Properties();
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, MyConfig.HOST_AND_PORT);
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"g01");
        //选配的
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        //先将数据消费出来
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);


        consumer.subscribe(Arrays.asList("users"), new ConsumerRebalanceListener() {

            /*
            消费 0 1 2  在分配之前该消费者消费什么分区
             */
            @Override
            public void onPartitionsRevoked(Collection<TopicPartition> partitions) {

            }

            /**
             *  1 2 3 4
             * 消费者组在均衡分配后的结果==》 分配之后该消费者应该消费哪些分区的数据
             */
            @Override
            public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
                for (TopicPartition partition : partitions) {
                    try {
                        int par = partition.partition();
                        String topic = partition.topic();
                        //这个消费者到底消费的哪些分区，是放在一个集合中的
                        get_offset.setString(1,topic);
                        get_offset.setInt(2,par);
                        ResultSet resultSet = get_offset.executeQuery();
                        if (resultSet.next()){
                            long offset = resultSet.getLong("offset");
                            consumer.seek(partition,offset+1);
                        }else {
                            consumer.seek(partition,0);
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
      /*  TopicPartition topicPartition0 = new TopicPartition("users", 0);
        TopicPartition topicPartition1 = new TopicPartition("users", 1);
        TopicPartition topicPartition2 = new TopicPartition("users", 2);
        consumer.assign(Arrays.asList(topicPartition0,topicPartition1,topicPartition2));

        //获取第一个分区的偏移量
        get_offset.setInt(1,0);
        ResultSet resultSet = get_offset.executeQuery();
        if(resultSet.next()){
            System.out.println("走的是这边马？？");
            long offset = resultSet.getLong("offset");
            consumer.seek(topicPartition0,offset+1);
        }else {
            System.out.println("还是走的这边啊");
            consumer.seek(topicPartition0,0);
        }
       //获取第二个分区的偏移量
        get_offset.setInt(1,1);
        ResultSet resultSet1 = get_offset.executeQuery();

        if(resultSet1.next()){
            System.out.println("走的是这边马？？");
            long offset = resultSet1.getLong("offset");
            consumer.seek(topicPartition1,offset+1);
        }else {
            System.out.println("还是走的这边啊");
            consumer.seek(topicPartition1,0);
        }
       //获取第三个分区的偏移量
        get_offset.setInt(1,2);
        ResultSet resultSet2 = get_offset.executeQuery();

        if(resultSet2.next()){
            System.out.println("走的是这边马？？");
            long offset = resultSet2.getLong("offset");
            consumer.seek(topicPartition2,offset+1);
        }else {
            System.out.println("还是走的这边啊");
            consumer.seek(topicPartition2,0);
        }*/







        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            for (ConsumerRecord<String, String> consumerRecord : poll) {

                try {
                    //kafka里面存的是什么格式
                    //1,zss,18,male
                    String value = consumerRecord.value();
                    String[] arr = value.split(",");
                    int id = Integer.parseInt(arr[0]);
                    String name = arr[1];
                    int age = Integer.parseInt(arr[2]);
                    String gender = arr[3];
                    //往mysql数据库中写
                    pps.setInt(1,id);
                    pps.setString(2,name);
                    pps.setInt(3,age);
                    pps.setString(4,gender);
                    pps.execute();

                    String topic = consumerRecord.topic();
                    int partition = consumerRecord.partition();
                    long offset = consumerRecord.offset();
                    //把偏移量也存储到mysql中去
                    offset_pps.setString(1,"g01");
                    offset_pps.setString(2,topic);
                    offset_pps.setInt(3,partition);
                    offset_pps.setLong(4,offset);
                    offset_pps.setLong(5,offset);
                    offset_pps.execute();
                    //提交事务
                    conn.commit();
                } catch (Exception e) {
                    //回滚事务
                    conn.rollback();
                    e.printStackTrace();
                }


            }
        }
    }
}
